package edu.ihs.lhpy.action;

import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import edu.ihs.lhpy.base.BaseAction;
import edu.ihs.lhpy.domain.Institute;
import edu.ihs.lhpy.domain.News;
import edu.ihs.lhpy.domain.PageBean;
@Controller
@Scope("prototype")
public class NewsAction extends BaseAction<News>{

	private Long instituteId;
	
	//列表
	public String list() throws Exception{
//		List<News> newsList = newsService.findAll();
		PageBean pageBean = newsService.getPageBean(pageNum);
		ActionContext.getContext().getValueStack().push(pageBean);
		
 		return "list";
	}
	//添加
	public String add() throws Exception{
		model.setInstitute(instituteService.getById(instituteId));
		newsService.save(model);
		return "toList";
	}
	//添加界面
	public String addUI() throws Exception{
		List<Institute> instituteList = instituteService.findAll();
		ActionContext.getContext().put("instituteList", instituteList);
		return "addUI";
	}
	//删除
	public String delete() throws Exception{
		newsService.delete(model.getId());
		return "toList";
	}
	//修改
	public String edit() throws Exception{
		News news = newsService.getById(model.getId());
		
		news.setAuthor(model.getAuthor());
		news.setContent(model.getContent());
		news.setInstitute(instituteService.getById(instituteId));
		news.setShotTitle(model.getShotTitle());
		news.setTime(model.getTime());
		news.setTitle(model.getTitle());
		//更新到数据库中
		newsService.update(news);
		return "toList";
	}
	//修改界面
	public String editUI() throws Exception{
		//准备回显数据
		List<Institute> instituteList = instituteService.findAll();
		ActionContext.getContext().put("instituteList", instituteList);
		News news = newsService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(news);
		return "editUI";
	}
	public Long getInstituteId() {
		return instituteId;
	}
	public void setInstituteId(Long instituteId) {
		this.instituteId = instituteId;
	}
	
	
	//传递的pageNum默认为第一页
	private int pageNum = 1;
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	
	
}
